package com.etheart.lyric.htmlparser.action.parse;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.htmlparser.Parser;
import org.htmlparser.util.ParserException;

import com.etheart.lyric.htmlparser.service.baidu.BaiduAlbumListParseService;
import com.etheart.lyric.htmlparser.service.baidu.BaiduAlbumParseService;
import com.etheart.lyric.htmlparser.service.baidu.BaiduSingerListParseService;
import com.etheart.lyric.htmlparser.service.baidu.BaiduSingerParseService;
import com.etheart.lyric.htmlparser.service.baidu.BaiduSongParseService;
import com.etheart.lyric.htmlparser.util.HtmlparserUtils;
import com.etheart.model.entity.Album;
import com.etheart.model.entity.Resource;
import com.etheart.model.entity.Singer;
import com.etheart.model.entity.Song;
import com.etheart.music.service.AlbumService;
import com.etheart.music.service.ResourceService;
import com.etheart.music.service.SingerService;
import com.etheart.music.service.SongService;
import com.sun.org.apache.bcel.internal.generic.NEW;

public class BaiduAttemperThread extends Thread {
	private static final Log logger = LogFactory.getLog(BaiduAttemperThread.class);
	public BaiduSingerParseService singerParseService;
	public BaiduSingerListParseService singerListParseService;
	public BaiduAlbumParseService albumParseService;
	public BaiduAlbumListParseService albumListParseService;
	public BaiduSongParseService songParseService;
	
	public SingerService singerService;
	public AlbumService albumService;
	public SongService songService;
	public ResourceService resourceService;
	
	private List<Object[]> singerObjectList = new ArrayList<Object[]>();
	public String rootDir;
	
	public String songNetId = null;
	public String singerNetId = null;
	public String albumNetId = null;
	
	
	public boolean foundSingerBreak = false;
	public boolean foundAlbumBreak = false;
	public boolean foundSongBreak = false;
	public boolean isMatchSingerSong = false;
	
	private String rootUrl="http://ting.baidu.com/";
	@Override
	public synchronized void start() {
		
		System.out.println("歌手列表解析完毕");
		
		
		if (StringUtils.isBlank(singerNetId)) {
			foundSingerBreak = true;
		}
		if (StringUtils.isBlank(albumNetId )) {
			foundAlbumBreak = true;
		}
		if (StringUtils.isBlank(songNetId)) {
			foundSongBreak = true;
		}
		
		super.start();
	}
	
	private String curNetId;
	public List<String> singerids;
	public String areaAndSex;
	@Override
	public void run() {
		initParseThreads();
		for (String id : singerids) {
			curNetId = id;
			StaticState.cur_singer = curNetId;
			System.out.println(areaAndSex+":"+curNetId);
			// 找到断点
			if (!foundSingerBreak) {
				if (curNetId.replace("/artist/", "").equals(singerNetId)) {
					foundSingerBreak = true;
				} else {
					continue;
				}
			}
			saveSinger(curNetId, areaAndSex);
			SingerId.singerMap.put(areaAndSex, curNetId.replace("/artist/", ""));
		}
	}

	private void saveSinger(String curNetId2,String areaAndSex) {
		//解析歌手
		curNetId2 = curNetId2.replace("artist/", "");
		Parser singerParser = HtmlparserUtils.getParser(rootDir+"/artist/"+curNetId2+"/introduction.html", rootUrl+"artist"+curNetId2+"/introduction");
		Singer singer = singerParseService.getSingerFromParser(singerParser, curNetId2);
		if (singerService.findByNetId(singer.getNetId())==null) {
			String[] str = areaAndSex.split("-");
			singer.setArea(str[0]);
			singer.setSex(str[1]);
			singerService.addSinger(singer);
		}else{
			singer = singerService.findByNetId(singer.getNetId());
		}
		
		//解析专辑列表
		Parser albumListparser = HtmlparserUtils.getParser(rootDir+"/artist/"+curNetId2+"/album.html", rootUrl+"artist"+curNetId2+"/album");
		try {
			List<String> albumUrlList = albumListParseService.parseAlbumListPage(albumListparser, rootDir+"/artist/"+curNetId2+"/album.html", rootUrl+"artist"+curNetId2+"/album");
			for (String url : albumUrlList) {
				String albumid = url.replace("/album/", "");
				Parser albumParser = HtmlparserUtils.getParser(rootDir+"/album/"+albumid+".html", rootUrl+"album/"+albumid);
				Album album = saveAlbum(albumParser,singer, albumid);
				saveSong(albumParser,albumid,singer,album);
			}
		} catch (ParserException e) {
			logger.error("eeeeeeeeeeeeeeeeeeee:专辑列表解析失败,singerNetId:"+singerNetId);
		}
	}


	private void initParseThreads() {
		
	}
	private void saveSong(Parser albumParser,String albumid,Singer singer , Album album )  {
		//解析歌曲
		try {
			List<Song> songs = albumParseService.getSongs(albumParser);
			for (Song song : songs) {
				//http://ting.baidu.com/song/13736238
				song.setAlbum(album);
				song.setSinger(singer);
				song.setSingername(singer.getSingername());
				song.setAlbumname(album.getAlbumname());
				song.setStyle(album.getLanguage());
				Parser songParser = HtmlparserUtils.getParserFromNetWork( rootUrl+"song/"+song.getNetId());
				if (songParser == null) {
					continue;
				}
				songParseService.fillSong(songParser, song);
			}
			
			for (Song song : songs) {
				if (songService.findByNetId(song.getNetId())==null) {
					songService.addSong(song);
				}
			}
			
		} catch (Exception e) {
			logger.error("eeeeeeeeeeeeeeee:歌曲解析异常:albumnetid:"+albumid);
			e.printStackTrace();
		}
		
	}
	private Album saveAlbum(Parser parser,Singer singer, String albumid) {
		//解析专辑
		Album album = albumParseService.getAlbum(parser, albumid);
		album.setSinger(singer);
		if (albumService.findByNetId(album.getNetId())==null) {
			albumService.addAlbum(album);
		}else{
			album = albumService.findByNetId(album.getNetId());
		}
		return album;
	}

}
